// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  onError (msg) {
    console.log(msg)
  },
  globalData: {
    userInfo: null,
    tabBar: {
      "color": "#999",
          "selectedColor": "#3D98FF",
          "backgroundColor": "#fff",
          "borderStyle": "black",
      "list": [
        {
          "pagePath": "pages/home/home",
          "text":"首页",
          "iconPath": "/resources/home/home.png",
          "selectedIconPath": "/resources/home/home-after.png"
        },
        {
          "pagePath": "pages/classification/classification",
          "text":"分类",
          "iconPath": "/resources/home/classify.png",
          "selectedIconPath": "/resources/home/classify-fill.png"
        },
        {
          "pagePath": "pages/messages/messages",
          "text":"消息",
          "iconPath": "/resources/home/message.png",
          "selectedIconPath": "/resources/home/message-after.png"
        },
        {
          "pagePath": "pages/personage/personage",
          "text":"我",
          "iconPath": "/resources/home/user.png",
          "selectedIconPath": "/resources/home/user-after.png"
        }
      ]
    }
  },
  editTabBar: function () {
    //使用getCurrentPages可以获取当前加载中所有的页面对象的一个数组，数组最后一个就是当前页面。
    var curPageArr = getCurrentPages();    //获取加载的页面
    var curPage = curPageArr[curPageArr.length - 1];    //获取当前页面的对象
    var pagePath = curPage.route;    //当前页面url
    if (pagePath.indexOf('/') != 0) {
      pagePath = '/' + pagePath;
    }
    
    var tabBar = this.globalData.tabBar;
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (tabBar.list[i].pagePath == pagePath) {
        tabBar.list[i].active = true;    //根据页面地址设置当前页面状态    
      }
    }
    curPage.setData({
      tabBar: tabBar
    });
  }
})
